﻿namespace ThingsGateway.Foundation
{
    public class LogMessageItem : EventArgs
    {
        private static long IdNumber;
        public LogMessageItem()
        {
            Id = Interlocked.Increment(ref IdNumber);
            Time = DateTime.Now;
        }

        /// <summary>
        /// 是否取消写入
        /// </summary>
        public bool Cancel { get; set; }

        /// <summary>
        /// 消息等级
        /// </summary>
        public LogType Degree { get; set; } = LogType.Debug;

        /// <summary>
        /// 单个记录信息的标识ID
        /// </summary>
        public long Id { get; private set; }

        /// <summary>
        /// 消息关键字
        /// </summary>
        public string KeyWord { get; set; }

        /// <summary>
        /// 消息文本
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// 线程ID
        /// </summary>
        public int ThreadId { get; set; }

        /// <summary>
        /// 记录日志的时间
        /// </summary>
        public DateTime Time { get; set; }

        /// <inheritdoc />
        public override string ToString()
        {
            if (Degree == LogType.None)
            {
                return Text;
            }
            return string.IsNullOrEmpty(KeyWord) ?
                string.Format("[{1:yyyy-MM-dd HH:mm:ss.fff}] [Thread-{2:D3}] [{0}] {3}",
                LogNetExtensions.GetDegreeDescription(Degree), Time, ThreadId, Text) :
                string.Format("[{1:yyyy-MM-dd HH:mm:ss.fff}] [Thread-{2:D3}] [{0}] [{3}] {4}",
                LogNetExtensions.GetDegreeDescription(Degree), Time, ThreadId, KeyWord, Text);
        }
    }
}